<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/4/29 15:09
 * Remark:
 */

namespace App\ServiceChecker\Client;

use App\Consts\BaseConsts\BaseConsts;
use App\Consts\ClientConsts\ClientConsts;
use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\ResponseFunc;
use App\Models\Client\ClientModel;
use App\Models\Client\CreditClientModel;
use App\Models\CreditProject\CreditProjectModel;
use App\ServiceChecker\BasicServiceChecker;
use Illuminate\Http\Exceptions\HttpResponseException;

class ClientLegitimacy extends BasicServiceChecker
{
    public ClientModel $_clientBasicModel;
    public CreditProjectModel $_creditProjectModel;
    public CreditClientModel $creditClientModel;

    public function __construct()
    {
        parent::__construct();
        $this->_clientBasicModel = new ClientModel();
        $this->_creditProjectModel = new CreditProjectModel();
        $this->creditClientModel = new CreditClientModel();
    }

    /**
     * 校验公司名称是否存在
     * @param $companyName
     */
    public function verifyCompanyIsExists($companyId){
        $isExist = $this->_companyModel->isExistByCondition([
            'company_id' => $companyId,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if(!$isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "当前公司不存在",
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,

            ));
        }
    }

    /**
     * 校验公司名称是否存在
     * @param $companyName
     */
    public function verifyCompanyClientNameIsExists($companyId,$clientName,$client_counterparty_type){
        $isExist = $this->_clientBasicModel->isExistByCondition([
            'company_id' => $companyId,
            'client_name' => $clientName,
            'client_counterparty_type' => $client_counterparty_type,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if($isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "当前客户已存在",
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }

    public function verifyClientStatus($clientId,$processStatus){
        $isExist = $this->creditClientModel->isExistByCondition([
            'client_id' => $clientId,
            'audit_status' => $processStatus,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if(!$isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "客户不存在或已审批通过",
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }

    /**
     * @param $clientId
     * @param string $msg
     */
    public function verifyClientIsExists($clientId, string $msg = "客户不存在"){
        $isExist = $this->_clientBasicModel->isExistByCondition([
            'client_id' => $clientId,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if(!$isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                $msg,
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }

    public function verifyCreditClientIsBlack($clientId){
        $isExist = $this->_clientBasicModel->isExistByCondition([
            'client_id' => $clientId,
            'client_type' => ClientConsts::CREDIT_CLIENT_BLACK_TYPE
        ]);
        if(!$isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "当前客户为黑名单客户",
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }



    public function verifyCreditProjectTotalIsGrClientCreditTotal($clientId,$creditClientLimit){
        $projectTotal =  $this->_creditProjectModel->getCreditProjectTotal($clientId);
        if($creditClientLimit < $projectTotal){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "已有信用项目额度大于客户额度",
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }

    /**
     * 校验当前客户是否属于信用客户
     * @param $client_id
     * @return void
     */
    public function verifyClientIsCreditClientCanTouchContract($client_id)
    {
        $clientInfo = $this->_clientBasicModel->getClientInfoByClientId($client_id,['is_credit_client'])['is_credit_client'];
        if($clientInfo!=1){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "相对方不属于信用客户禁止创建预付合同",
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }

    public function verifyClientIsUpstreamCreditClient($clientId){
        $clientInfo = $this->_clientBasicModel->getClientInfoByClientId($clientId,['client_industry_type','is_credit_client']);
        if($clientInfo['is_credit_client']!=1 || $clientInfo['client_industry_type']!=2){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "当前客户不属于上游信用客户",
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }

    public function verifyClientIsDownstreamCreditClient($clientId){
        $clientInfo = $this->_clientBasicModel->getClientInfoByClientId($clientId,['client_counterparty_type','client_industry_type','is_credit_client']);

        if($clientInfo['is_credit_client']!=1 || $clientInfo['client_counterparty_type']!=1){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "当前客户不属于下游信用客户",
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }
}